package com.zhangyuji.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangyuji.dto.SetmealDto;
import com.zhangyuji.entity.Category;
import com.zhangyuji.entity.Setmeal;
import com.zhangyuji.entity.SetmealDish;
import com.zhangyuji.mapper.CategoryMapper;
import com.zhangyuji.mapper.SetmealMapper;
import com.zhangyuji.service.CategoryService;
import com.zhangyuji.service.SetmealDishService;
import com.zhangyuji.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public boolean addSetmealAndSetmealDish(SetmealDto setmealDto) {
        //保存操作,把前端数据保存到数据库setmeal表中
        boolean saveSetmealDish = this.save(setmealDto);
        if (saveSetmealDish){
            //保存操作,把前端数据保存到数据库setmeal_dish表中
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            List<SetmealDish> setmealDishList = new ArrayList<>();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDto.getId());
                setmealDishList.add(setmealDish);
            }
            boolean saveBatch = setmealDishService.saveBatch(setmealDishList);
            if (saveBatch){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 单个/批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean deleteSetmealAndSetmealDish(Long[] ids) {
        for (Long id : ids) {
            boolean removeSetmeal = this.removeById(id);
            if (removeSetmeal){
                //根据setmeal_id删除setmeal_dish记录
                LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SetmealDish::getSetmealId,id);
                boolean removeSetmealDish = setmealDishService.remove(queryWrapper);
                if (removeSetmealDish){
                    return true;
                }
                return false;
            }
        }
        return false;
    }

    /**
     * 编辑套餐,查询回显数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto querySetmealById(Long id) {
        Setmeal setmeal = this.getById(id);
        if (setmeal != null){
            //根据id查询setmeal_dish的数据
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

            if (setmealDishes.size() > 0){
                SetmealDto setmealDto = new SetmealDto();
                BeanUtils.copyProperties(setmeal,setmealDto);
                setmealDto.setSetmealDishes(setmealDishes);
                return setmealDto;
            }
            return null;

        }

        return null;
    }


    /**
     * 编辑套餐,保存到数据库
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public boolean editSetmeal(SetmealDto setmealDto) {
        //setmeal数据保存到setmeal表
        boolean updateSetmeal = this.updateById(setmealDto);
        if (updateSetmeal){
            //setmealDish数据保存到setmeal_dish表
                //先删除
            Long setmealId = setmealDto.getId();
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
            boolean removeSetmealDish = setmealDishService.remove(queryWrapper);

            if (removeSetmealDish){
                //再添加
                List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
                for (SetmealDish setmealDish : setmealDishList) {
                    setmealDish.setSetmealId(setmealId);
                }
                boolean saveBatch = setmealDishService.saveBatch(setmealDishList);
                if (saveBatch){
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }
}
